package cn.yedong.order.service.impl;

import cn.yedong.basic.util.LoginContext;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.order.domain.OrderPetAcquisition;
import cn.yedong.order.dto.OrderDto;
import cn.yedong.order.mapper.OrderPetAcquisitionMapper;
import cn.yedong.order.query.OrderPetAcquisitionQuery;
import cn.yedong.order.service.IOrderPetAcquisitionService;
import cn.yedong.basic.service.impl.BaseServiceImpl;
import cn.yedong.org.domain.Employee;
import cn.yedong.org.domain.Shop;
import cn.yedong.org.mapper.EmployeeMapper;
import cn.yedong.org.mapper.ShopMapper;
import cn.yedong.pet.domain.Pet;
import cn.yedong.pet.domain.SearchMasterMsg;
import cn.yedong.pet.mapper.PetMapper;
import cn.yedong.pet.mapper.SearchMasterMsgMapper;
import cn.yedong.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    /**
     * 分页查询：方法重载 - 增加参数，查询当前登录信息
     * @param query
     * @param request
     * @return
     */
    @Override
    public QueryResult<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //获取当前用户登陆信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //根据登录信息id查询员工
        Employee employee = employeeMapper.findByLoginfoId(logininfo.getId());
        if(employee.getShop_id() != null){//店长 or 店员
            //查询其所在的店铺
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdminId() != employee.getId()){//员工
                //给分页查询参数设置empId
                query.setEmpId(employee.getId());
            }
            //给分页参数设置shopId
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    /**
     * 订单管理 - 取消订单
     * id - 寻主消息id
     * @param id
     */
    @Override
    @Transactional
    public void cancel(Long id) {
        //1.将寻主订单放入寻主池
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //修改订单状态 取消  state=-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    /**
     * 订单管理 - 确认订单
     * @param orderDto
     */
    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        //获取参数
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();//收购价
        Integer payType = orderDto.getPayType();//收购时支付方式
        //1.生成宠物对象
        //获取订单对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        //获取寻主消息对象
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        //生成宠物对象
        Pet pet = createPet(msg,money);
        petMapper.add(pet);

        //2.修改订单状态 - 修改金额（收购价） -修改支付方式
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息状态  state=2 寻主消息完成
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    /**
     * 接收订单，根据  寻主消息+成交 价创建宠物对象
     * @param msg
     * @param money
     * @return
     */
    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
//        pet.setUserId(msg.getUserId());//领养人的id
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
